package api

import (
	"github.com/gin-gonic/gin"
	"go_api/model"
	"go_api/pkg/validator"
	"go_api/request"
	"math/rand"
)

type ProjectController struct {
	BaseController
}

// GetGroupList 获取所有分组数据
func (project ProjectController) GetProjectList(c *gin.Context) {

	//数据验证
	var data request.GetProjectListRequest
	msg, ok := validator.ValidatorData(c, &data)
	if !ok {
		project.responseError(c, 1, msg)
		return
	}
	projectModel := model.XProject{}
	items := projectModel.GetProjectList(data.GroupId)

	project.responseSuccess(c, items)
}

// CreateProject 新增项目
func (project ProjectController) CreateProject(c *gin.Context) {
	colors := []string{"red", "blue", "green", "yellow", "orange", "purple", "teal", "pink", "indigo", "amber", "cyan", "lime", "brown", "deeporange", "lightblue", "grey", "black"}
	// 生成一个随机索引
	randomIndex := rand.Intn(len(colors))

	// 从切片中获取随机元素
	randomElement := colors[randomIndex]

	//数据验证
	var data request.CreateProjectRequest
	msg, ok := validator.ValidatorData(c, &data)
	if !ok {
		project.responseError(c, 1, msg)
		return
	}
	//数据
	projectModel := model.XProject{}
	projectModel.ProjectName = data.ProjectName
	projectModel.GroupID = data.GroupId
	projectModel.Color = randomElement
	projectModel.IsDel = 2

	err := projectModel.CreateProject()
	if err != nil {
		project.responseError(c, 1, "分组创建失败")
		return
	}

	project.responseSuccess(c, nil)
}

// GetProjectMenuList 获取项目分类数据
func (project ProjectController) GetProjectMenuList(c *gin.Context) {

	//数据验证
	var data request.GetProjectMenuListRequest
	msg, ok := validator.ValidatorData(c, &data)
	if !ok {
		project.responseError(c, 1, msg)
		return
	}
	projectMenuModel := model.XProjectMenu{}
	items := projectMenuModel.GetProjectMenuList(data.ProjectId, data.Search)

	interfaceModel := model.XInterface{}
	for key := range items {
		_ = interfaceModel.GetInterfaceList(&items[key])
	}
	//result := make([]map[string]interface{}, 0)
	//
	//for _, v := range items {
	//	item := make(map[string]interface{})
	//	children := interfaceModel.GetInterfaceList(v.ID)
	//
	//	item["id"] = v.ID
	//	item["name"] = v.Name
	//	item["children"] = children
	//	result = append(result, item)
	//}
	project.responseSuccess(c, items)
}

// CreateProjectMenu 新增项目分类
func (project ProjectController) CreateProjectMenu(c *gin.Context) {
	//数据验证
	var data request.CreateProjectMenuRequest
	msg, ok := validator.ValidatorData(c, &data)
	if !ok {
		project.responseError(c, 1, msg)
		return
	}
	//数据
	projectMenuModel := model.XProjectMenu{}
	projectMenuModel.Name = data.Name
	projectMenuModel.ProjectID = data.ProjectId
	projectMenuModel.IsDel = 2

	err := projectMenuModel.CreateProjectMenu()
	if err != nil {
		project.responseError(c, 1, "分类创建失败")
		return
	}

	project.responseSuccess(c, nil)
}

// CreateProjectMenu 新增项目分类
func (project ProjectController) EditProjectMenu(c *gin.Context) {
	//数据验证
	var data request.EditProjectMenuRequest
	msg, ok := validator.ValidatorData(c, &data)
	if !ok {
		project.responseError(c, 1, msg)
		return
	}
	//数据
	projectMenuModel := model.XProjectMenu{}

	err := projectMenuModel.UpdateProjectMenu(data.MenuId, data.Name)
	if err != nil {
		project.responseError(c, 1, "更新失败")
		return
	}

	project.responseSuccess(c, nil)
}

// DeleteProjectMenu 删除分类
func (project ProjectController) DeleteProjectMenu(c *gin.Context) {
	//数据验证
	var data request.DelProjectMenuRequest
	msg, ok := validator.ValidatorData(c, &data)
	if !ok {
		project.responseError(c, 1, msg)
		return
	}

	projectMenuModel := model.XProjectMenu{}
	//查询记录是否存在
	projectMenuInfo, err := projectMenuModel.GetProjectMenuInfo(data.MenuId)
	if err != nil {
		project.responseError(c, 1, "查找失败")
		return
	}
	if projectMenuInfo == nil || projectMenuInfo.ID <= 0 {
		project.responseError(c, 1, "接口不存在")
		return
	}
	if projectMenuInfo.ProjectID != data.ProjectId {
		project.responseError(c, 1, "接口归属项目错误")
		return
	}
	//删除记录
	err = projectMenuModel.DeleteProjectMenu(projectMenuInfo.ID)
	if err != nil {
		project.responseError(c, 1, "删除失败")
		return
	}
	project.responseSuccess(c, nil)
}
